﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;

namespace SmartMathLibrary.Interfaces.Web.Twitter
{
    /// <summary>
    /// This class provides an interface to twitter.
    /// </summary>
    [Serializable]
    public class Twitter
    {
        /// <summary>
        /// This field holds the connection information for the twitter login.
        /// </summary>
        private Connection connection;

        /// <summary>
        /// Initializes a new instance of the <see cref="Twitter"/> class.
        /// </summary>
        /// <param name="connection">The connection information for the twitter login.</param>
        public Twitter(Connection connection)
        {
            if (connection == (Connection) null)
            {
                throw new ArgumentNullException("connection");
            }

            this.connection = connection;
        }

        /// <summary>
        /// Gets or sets the connection information for the twitter login.
        /// </summary>
        /// <value>The connection information for the twitter login.</value>
        public Connection Connection
        {
            get { return connection; }
            set { connection = value; }
        }

        /// <summary>
        /// Gets the followers of the specified twitter account.
        /// </summary>
        /// <returns>The followers of the specified twitter account.</returns>
        public List<User> GetFollowers()
        {
            List<User> users = new List<User>();
            TwitterCore core = new TwitterCore();
            XmlDocument document = new XmlDocument();
            String webResult = core.GetFollowers(this.connection.UserName, this.connection.Password,
                                                 TwitterCore.OutputFormatType.XML);

            if (webResult == null)
            {
                return new List<User>();
            }

            if (webResult.Trim().Length == 0)
            {
                return new List<User>();
            }

            document.LoadXml(webResult);

            XmlNodeList xmlUsers = document.GetElementsByTagName("user");

            for (int i = 0; i < xmlUsers.Count; i++)
            {
                User tempuri = new User();

                tempuri.Id = xmlUsers[i]["id"].InnerText;
                tempuri.Name = xmlUsers[i]["name"].InnerText;
                tempuri.ScreenName = xmlUsers[i]["screen_name"].InnerText;
                tempuri.Location = xmlUsers[i]["location"].InnerText;
                tempuri.Description = xmlUsers[i]["description"].InnerText;
                tempuri.ProfileImageUrl = xmlUsers[i]["profile_image_url"].InnerText;
                tempuri.Url = xmlUsers[i]["url"].InnerText;
                tempuri.ProtectedStatus = Boolean.Parse(xmlUsers[i]["protected"].InnerText);
                tempuri.FollowersCount = Int32.Parse(xmlUsers[i]["followers_count"].InnerText);
                tempuri.ProfileBackgroundColor = xmlUsers[i]["profile_background_color"].InnerText;
                tempuri.ProfileTextColor = xmlUsers[i]["profile_text_color"].InnerText;
                tempuri.ProfileLinkColor = xmlUsers[i]["profile_link_color"].InnerText;
                tempuri.ProfileSidebarFillColor = xmlUsers[i]["profile_sidebar_fill_color"].InnerText;
                tempuri.ProfileSidebarBorderColor = xmlUsers[i]["profile_sidebar_border_color"].InnerText;
                tempuri.FriendsCount = Int32.Parse(xmlUsers[i]["friends_count"].InnerText);
                tempuri.CreatedAt = xmlUsers[i]["created_at"].InnerText;
                tempuri.FavouritesCount = Int32.Parse(xmlUsers[i]["favourites_count"].InnerText);
                tempuri.UtcOffset = xmlUsers[i]["utc_offset"].InnerText;
                tempuri.TimeZone = xmlUsers[i]["time_zone"].InnerText;
                tempuri.ProfileBackgroundImageUrl = xmlUsers[i]["profile_background_image_url"].InnerText;
                tempuri.ProfileBackgroundTile = Boolean.Parse(xmlUsers[i]["profile_background_tile"].InnerText);
                tempuri.StatusesCount = Int32.Parse(xmlUsers[i]["statuses_count"].InnerText);
                tempuri.Notifications = xmlUsers[i]["notifications"].InnerText;
                tempuri.Verified = Boolean.Parse(xmlUsers[i]["verified"].InnerText);

                users.Add(tempuri);
            }

            return users;
        }

        /// <summary>
        /// Gets the followers of the specified twitter account.
        /// </summary>
        /// <returns>The followers of the specified twitter account.</returns>
        public List<User> GetFollowers(String screenName)
        {
            List<User> users = new List<User>();
            TwitterCore core = new TwitterCore();
            XmlDocument document = new XmlDocument();
            String webResult = core.GetFollowers(this.connection.UserName, this.connection.Password,screenName,
                                                 TwitterCore.OutputFormatType.XML);

            if (webResult == null)
            {
                return new List<User>();
            }

            if (webResult.Trim().Length == 0)
            {
                return new List<User>();
            }

            document.LoadXml(webResult);

            XmlNodeList xmlUsers = document.GetElementsByTagName("user");

            for (int i = 0; i < xmlUsers.Count; i++)
            {
                User tempuri = new User();

                tempuri.Id = xmlUsers[i]["id"].InnerText;
                tempuri.Name = xmlUsers[i]["name"].InnerText;
                tempuri.ScreenName = xmlUsers[i]["screen_name"].InnerText;
                tempuri.Location = xmlUsers[i]["location"].InnerText;
                tempuri.Description = xmlUsers[i]["description"].InnerText;
                tempuri.ProfileImageUrl = xmlUsers[i]["profile_image_url"].InnerText;
                tempuri.Url = xmlUsers[i]["url"].InnerText;
                tempuri.ProtectedStatus = Boolean.Parse(xmlUsers[i]["protected"].InnerText);
                tempuri.FollowersCount = Int32.Parse(xmlUsers[i]["followers_count"].InnerText);
                tempuri.ProfileBackgroundColor = xmlUsers[i]["profile_background_color"].InnerText;
                tempuri.ProfileTextColor = xmlUsers[i]["profile_text_color"].InnerText;
                tempuri.ProfileLinkColor = xmlUsers[i]["profile_link_color"].InnerText;
                tempuri.ProfileSidebarFillColor = xmlUsers[i]["profile_sidebar_fill_color"].InnerText;
                tempuri.ProfileSidebarBorderColor = xmlUsers[i]["profile_sidebar_border_color"].InnerText;
                tempuri.FriendsCount = Int32.Parse(xmlUsers[i]["friends_count"].InnerText);
                tempuri.CreatedAt = xmlUsers[i]["created_at"].InnerText;
                tempuri.FavouritesCount = Int32.Parse(xmlUsers[i]["favourites_count"].InnerText);
                tempuri.UtcOffset = xmlUsers[i]["utc_offset"].InnerText;
                tempuri.TimeZone = xmlUsers[i]["time_zone"].InnerText;
                tempuri.ProfileBackgroundImageUrl = xmlUsers[i]["profile_background_image_url"].InnerText;
                tempuri.ProfileBackgroundTile = Boolean.Parse(xmlUsers[i]["profile_background_tile"].InnerText);
                tempuri.StatusesCount = Int32.Parse(xmlUsers[i]["statuses_count"].InnerText);
                tempuri.Notifications = xmlUsers[i]["notifications"].InnerText;
                tempuri.Verified = Boolean.Parse(xmlUsers[i]["verified"].InnerText);

                users.Add(tempuri);
            }

            return users;
        }

        /// <summary>
        /// Gets the followers of the specified twitter account.
        /// </summary>
        /// <param name="screenName">The screenname of the twitter account.</param>
        /// <param name="page">The page number to grab.</param>
        /// <returns>
        /// The followers of the specified twitter account.
        /// </returns>
        public List<User> GetFollowers(String screenName, int page)
        {
            List<User> users = new List<User>();
            TwitterCore core = new TwitterCore();
            XmlDocument document = new XmlDocument();
            String webResult = core.GetFollowers(this.connection.UserName, this.connection.Password, screenName,page,
                                                 TwitterCore.OutputFormatType.XML);

            if (webResult == null)
            {
                return new List<User>();
            }

            if (webResult.Trim().Length == 0)
            {
                return new List<User>();
            }

            document.LoadXml(webResult);

            XmlNodeList xmlUsers = document.GetElementsByTagName("user");

            for (int i = 0; i < xmlUsers.Count; i++)
            {
                User tempuri = new User();

                tempuri.Id = xmlUsers[i]["id"].InnerText;
                tempuri.Name = xmlUsers[i]["name"].InnerText;
                tempuri.ScreenName = xmlUsers[i]["screen_name"].InnerText;
                tempuri.Location = xmlUsers[i]["location"].InnerText;
                tempuri.Description = xmlUsers[i]["description"].InnerText;
                tempuri.ProfileImageUrl = xmlUsers[i]["profile_image_url"].InnerText;
                tempuri.Url = xmlUsers[i]["url"].InnerText;
                tempuri.ProtectedStatus = Boolean.Parse(xmlUsers[i]["protected"].InnerText);
                tempuri.FollowersCount = Int32.Parse(xmlUsers[i]["followers_count"].InnerText);
                tempuri.ProfileBackgroundColor = xmlUsers[i]["profile_background_color"].InnerText;
                tempuri.ProfileTextColor = xmlUsers[i]["profile_text_color"].InnerText;
                tempuri.ProfileLinkColor = xmlUsers[i]["profile_link_color"].InnerText;
                tempuri.ProfileSidebarFillColor = xmlUsers[i]["profile_sidebar_fill_color"].InnerText;
                tempuri.ProfileSidebarBorderColor = xmlUsers[i]["profile_sidebar_border_color"].InnerText;
                tempuri.FriendsCount = Int32.Parse(xmlUsers[i]["friends_count"].InnerText);
                tempuri.CreatedAt = xmlUsers[i]["created_at"].InnerText;
                tempuri.FavouritesCount = Int32.Parse(xmlUsers[i]["favourites_count"].InnerText);
                tempuri.UtcOffset = xmlUsers[i]["utc_offset"].InnerText;
                tempuri.TimeZone = xmlUsers[i]["time_zone"].InnerText;
                tempuri.ProfileBackgroundImageUrl = xmlUsers[i]["profile_background_image_url"].InnerText;
                tempuri.ProfileBackgroundTile = Boolean.Parse(xmlUsers[i]["profile_background_tile"].InnerText);
                tempuri.StatusesCount = Int32.Parse(xmlUsers[i]["statuses_count"].InnerText);
                tempuri.Notifications = xmlUsers[i]["notifications"].InnerText;
                tempuri.Verified = Boolean.Parse(xmlUsers[i]["verified"].InnerText);

                users.Add(tempuri);
            }

            return users;
        }

        /// <summary>
        /// Gets the friends of the specified twitter account.
        /// </summary>
        /// <returns>
        /// The friends of the specified twitter account.
        /// </returns>
        public List<User> GetFriends()
        {
            return this.GetFriends(null);
        }

        /// <summary>
        /// Gets the friends of the specified twitter account.
        /// </summary>
        /// <returns>The friends of the specified twitter account.</returns>
        public List<User> GetFriends(string screenName)
        {
            List<User> users = new List<User>();
            TwitterCore core = new TwitterCore();
            XmlDocument document = new XmlDocument();
            String webResult = core.GetFriends(this.connection.UserName, this.connection.Password, screenName,
                                               TwitterCore.OutputFormatType.XML);

            if (webResult == null)
            {
                return new List<User>();
            }

            if (webResult.Trim().Length == 0)
            {
                return new List<User>();
            }

            document.LoadXml(webResult);

            XmlNodeList xmlUsers = document.GetElementsByTagName("user");

            for (int i = 0; i < xmlUsers.Count; i++)
            {
                User tempuri = new User();

                tempuri.Id = xmlUsers[i]["id"].InnerText;
                tempuri.Name = xmlUsers[i]["name"].InnerText;
                tempuri.ScreenName = xmlUsers[i]["screen_name"].InnerText;
                tempuri.Location = xmlUsers[i]["location"].InnerText;
                tempuri.Description = xmlUsers[i]["description"].InnerText;
                tempuri.ProfileImageUrl = xmlUsers[i]["profile_image_url"].InnerText;
                tempuri.Url = xmlUsers[i]["url"].InnerText;
                tempuri.ProtectedStatus = Boolean.Parse(xmlUsers[i]["protected"].InnerText);
                tempuri.FollowersCount = Int32.Parse(xmlUsers[i]["followers_count"].InnerText);
                tempuri.ProfileBackgroundColor = xmlUsers[i]["profile_background_color"].InnerText;
                tempuri.ProfileTextColor = xmlUsers[i]["profile_text_color"].InnerText;
                tempuri.ProfileLinkColor = xmlUsers[i]["profile_link_color"].InnerText;
                tempuri.ProfileSidebarFillColor = xmlUsers[i]["profile_sidebar_fill_color"].InnerText;
                tempuri.ProfileSidebarBorderColor = xmlUsers[i]["profile_sidebar_border_color"].InnerText;
                tempuri.FriendsCount = Int32.Parse(xmlUsers[i]["friends_count"].InnerText);
                tempuri.CreatedAt = xmlUsers[i]["created_at"].InnerText;
                tempuri.FavouritesCount = Int32.Parse(xmlUsers[i]["favourites_count"].InnerText);
                tempuri.UtcOffset = xmlUsers[i]["utc_offset"].InnerText;
                tempuri.TimeZone = xmlUsers[i]["time_zone"].InnerText;
                tempuri.ProfileBackgroundImageUrl = xmlUsers[i]["profile_background_image_url"].InnerText;
                tempuri.ProfileBackgroundTile = Boolean.Parse(xmlUsers[i]["profile_background_tile"].InnerText);
                tempuri.StatusesCount = Int32.Parse(xmlUsers[i]["statuses_count"].InnerText);
                tempuri.Notifications = xmlUsers[i]["notifications"].InnerText;
                tempuri.Verified = Boolean.Parse(xmlUsers[i]["verified"].InnerText);

                users.Add(tempuri);
            }

            return users;
        }

        /// <summary>
        /// Gets the friends of the specified twitter account.
        /// </summary>
        /// <param name="screenName">The screenname of the twitter account.</param>
        /// <param name="page">The page number to grab.</param>
        /// <returns>
        /// The friends of the specified twitter account.
        /// </returns>
        public List<User> GetFriends(string screenName, int page)
        {
            List<User> users = new List<User>();
            TwitterCore core = new TwitterCore();
            XmlDocument document = new XmlDocument();
            String webResult = core.GetFriends(this.connection.UserName, this.connection.Password, screenName,page,
                                               TwitterCore.OutputFormatType.XML);

            if (webResult == null)
            {
                return new List<User>();
            }

            if (webResult.Trim().Length == 0)
            {
                return new List<User>();
            }

            document.LoadXml(webResult);

            XmlNodeList xmlUsers = document.GetElementsByTagName("user");

            for (int i = 0; i < xmlUsers.Count; i++)
            {
                User tempuri = new User();

                tempuri.Id = xmlUsers[i]["id"].InnerText;
                tempuri.Name = xmlUsers[i]["name"].InnerText;
                tempuri.ScreenName = xmlUsers[i]["screen_name"].InnerText;
                tempuri.Location = xmlUsers[i]["location"].InnerText;
                tempuri.Description = xmlUsers[i]["description"].InnerText;
                tempuri.ProfileImageUrl = xmlUsers[i]["profile_image_url"].InnerText;
                tempuri.Url = xmlUsers[i]["url"].InnerText;
                tempuri.ProtectedStatus = Boolean.Parse(xmlUsers[i]["protected"].InnerText);
                tempuri.FollowersCount = Int32.Parse(xmlUsers[i]["followers_count"].InnerText);
                tempuri.ProfileBackgroundColor = xmlUsers[i]["profile_background_color"].InnerText;
                tempuri.ProfileTextColor = xmlUsers[i]["profile_text_color"].InnerText;
                tempuri.ProfileLinkColor = xmlUsers[i]["profile_link_color"].InnerText;
                tempuri.ProfileSidebarFillColor = xmlUsers[i]["profile_sidebar_fill_color"].InnerText;
                tempuri.ProfileSidebarBorderColor = xmlUsers[i]["profile_sidebar_border_color"].InnerText;
                tempuri.FriendsCount = Int32.Parse(xmlUsers[i]["friends_count"].InnerText);
                tempuri.CreatedAt = xmlUsers[i]["created_at"].InnerText;
                tempuri.FavouritesCount = Int32.Parse(xmlUsers[i]["favourites_count"].InnerText);
                tempuri.UtcOffset = xmlUsers[i]["utc_offset"].InnerText;
                tempuri.TimeZone = xmlUsers[i]["time_zone"].InnerText;
                tempuri.ProfileBackgroundImageUrl = xmlUsers[i]["profile_background_image_url"].InnerText;
                tempuri.ProfileBackgroundTile = Boolean.Parse(xmlUsers[i]["profile_background_tile"].InnerText);
                tempuri.StatusesCount = Int32.Parse(xmlUsers[i]["statuses_count"].InnerText);
                tempuri.Notifications = xmlUsers[i]["notifications"].InnerText;
                tempuri.Verified = Boolean.Parse(xmlUsers[i]["verified"].InnerText);

                users.Add(tempuri);
            }

            return users;
        }

        /// <summary>
        /// Gets the timelines of the friends of the specified twitter account.
        /// </summary>
        /// <returns>The timelines of the friends of the specified twitter account.</returns>
        public List<TimelineItem> GetFriendsTimeline()
        {
            List<TimelineItem> timeline = new List<TimelineItem>();
            TwitterCore core = new TwitterCore();
            XmlDocument document = new XmlDocument();
            String webResult = core.GetFriendsTimeline(this.connection.UserName, this.connection.Password,
                                                       TwitterCore.OutputFormatType.XML);

            if (webResult == null)
            {
                return new List<TimelineItem>();
            }

            if (webResult.Trim().Length == 0)
            {
                return new List<TimelineItem>();
            }

            document.LoadXml(webResult);

            XmlNodeList xmlTimeline = document.GetElementsByTagName("status");

            for (int i = 0; i < xmlTimeline.Count; i++)
            {
                User user = new User();
                Status status = new Status();

                status.Id = xmlTimeline[i]["id"].InnerText;
                status.CreatedAt = xmlTimeline[i]["created_at"].InnerText;
                status.Text = xmlTimeline[i]["text"].InnerText;

                user.Id = xmlTimeline[i]["user"]["id"].InnerText;
                user.Name = xmlTimeline[i]["user"]["name"].InnerText;
                user.ScreenName = xmlTimeline[i]["user"]["screen_name"].InnerText;
                user.Location = xmlTimeline[i]["user"]["location"].InnerText;
                user.Description = xmlTimeline[i]["user"]["description"].InnerText;
                user.ProfileImageUrl = xmlTimeline[i]["user"]["profile_image_url"].InnerText;
                user.Url = xmlTimeline[i]["user"]["url"].InnerText;
                user.ProtectedStatus = Boolean.Parse(xmlTimeline[i]["user"]["protected"].InnerText);
                user.FollowersCount = Int32.Parse(xmlTimeline[i]["user"]["followers_count"].InnerText);
                user.ProfileBackgroundColor = xmlTimeline[i]["user"]["profile_background_color"].InnerText;
                user.ProfileTextColor = xmlTimeline[i]["user"]["profile_text_color"].InnerText;
                user.ProfileLinkColor = xmlTimeline[i]["user"]["profile_link_color"].InnerText;
                user.ProfileSidebarFillColor = xmlTimeline[i]["user"]["profile_sidebar_fill_color"].InnerText;
                user.ProfileSidebarBorderColor = xmlTimeline[i]["user"]["profile_sidebar_border_color"].InnerText;
                user.FriendsCount = Int32.Parse(xmlTimeline[i]["user"]["friends_count"].InnerText);
                user.CreatedAt = xmlTimeline[i]["user"]["created_at"].InnerText;
                user.FavouritesCount = Int32.Parse(xmlTimeline[i]["user"]["favourites_count"].InnerText);
                user.UtcOffset = xmlTimeline[i]["user"]["utc_offset"].InnerText;
                user.TimeZone = xmlTimeline[i]["user"]["time_zone"].InnerText;
                user.ProfileBackgroundImageUrl = xmlTimeline[i]["user"]["profile_background_image_url"].InnerText;
                user.ProfileBackgroundTile = Boolean.Parse(xmlTimeline[i]["user"]["profile_background_tile"].InnerText);
                user.StatusesCount = Int32.Parse(xmlTimeline[i]["user"]["statuses_count"].InnerText);
                user.Notifications = xmlTimeline[i]["user"]["notifications"].InnerText;
                user.Verified = Boolean.Parse(xmlTimeline[i]["user"]["verified"].InnerText);

                timeline.Add(new TimelineItem(user, status));
            }

            return timeline;
        }

        /// <summary>
        /// Gets the timeline of the specified twitter account.
        /// </summary>
        /// <returns>
        /// The timeline of the specified twitter account.
        /// </returns>
        public List<TimelineItem> GetUserTimeline()
        {
            return this.GetUserTimeline(null);
        }

        /// <summary>
        /// Gets the timeline of the specified twitter account.
        /// </summary>
        /// <param name="screenName">The screen name of the user from which the information should be grabbed.</param>
        /// <returns>
        /// The timeline of the specified twitter account.
        /// </returns>
        public List<TimelineItem> GetUserTimeline(string screenName)
        {
            List<TimelineItem> timeline = new List<TimelineItem>();
            TwitterCore core = new TwitterCore();
            XmlDocument document = new XmlDocument();
            String webResult = core.GetUserTimeline(this.connection.UserName, this.connection.Password, screenName,
                                                    TwitterCore.OutputFormatType.XML);

            if (webResult == null)
            {
                return new List<TimelineItem>();
            }

            if (webResult.Trim().Length == 0)
            {
                return new List<TimelineItem>();
            }

            document.LoadXml(webResult);

            XmlNodeList xmlTimeline = document.GetElementsByTagName("status");

            for (int i = 0; i < xmlTimeline.Count; i++)
            {
                User user = new User();
                Status status = new Status();

                status.Id = xmlTimeline[i]["id"].InnerText;
                status.CreatedAt = xmlTimeline[i]["created_at"].InnerText;
                status.Text = xmlTimeline[i]["text"].InnerText;

                user.Id = xmlTimeline[i]["user"]["id"].InnerText;
                user.Name = xmlTimeline[i]["user"]["name"].InnerText;
                user.ScreenName = xmlTimeline[i]["user"]["screen_name"].InnerText;
                user.Location = xmlTimeline[i]["user"]["location"].InnerText;
                user.Description = xmlTimeline[i]["user"]["description"].InnerText;
                user.ProfileImageUrl = xmlTimeline[i]["user"]["profile_image_url"].InnerText;
                user.Url = xmlTimeline[i]["user"]["url"].InnerText;
                user.ProtectedStatus = Boolean.Parse(xmlTimeline[i]["user"]["protected"].InnerText);
                user.FollowersCount = Int32.Parse(xmlTimeline[i]["user"]["followers_count"].InnerText);
                user.ProfileBackgroundColor = xmlTimeline[i]["user"]["profile_background_color"].InnerText;
                user.ProfileTextColor = xmlTimeline[i]["user"]["profile_text_color"].InnerText;
                user.ProfileLinkColor = xmlTimeline[i]["user"]["profile_link_color"].InnerText;
                user.ProfileSidebarFillColor = xmlTimeline[i]["user"]["profile_sidebar_fill_color"].InnerText;
                user.ProfileSidebarBorderColor = xmlTimeline[i]["user"]["profile_sidebar_border_color"].InnerText;
                user.FriendsCount = Int32.Parse(xmlTimeline[i]["user"]["friends_count"].InnerText);
                user.CreatedAt = xmlTimeline[i]["user"]["created_at"].InnerText;
                user.FavouritesCount = Int32.Parse(xmlTimeline[i]["user"]["favourites_count"].InnerText);
                user.UtcOffset = xmlTimeline[i]["user"]["utc_offset"].InnerText;
                user.TimeZone = xmlTimeline[i]["user"]["time_zone"].InnerText;
                user.ProfileBackgroundImageUrl = xmlTimeline[i]["user"]["profile_background_image_url"].InnerText;
                user.ProfileBackgroundTile = Boolean.Parse(xmlTimeline[i]["user"]["profile_background_tile"].InnerText);
                user.StatusesCount = Int32.Parse(xmlTimeline[i]["user"]["statuses_count"].InnerText);
                user.Notifications = xmlTimeline[i]["user"]["notifications"].InnerText;
                user.Verified = Boolean.Parse(xmlTimeline[i]["user"]["verified"].InnerText);

                timeline.Add(new TimelineItem(user, status));
            }

            return timeline;
        }

        /// <summary>
        /// Shows the specified status of the specified twitter account.
        /// </summary>
        /// <param name="screenName">The screen name of the user from which the information should be grabbed.</param>
        /// <returns>The current status of the specified twitter account.</returns>
        public TimelineItem Show(string screenName)
        {
            TimelineItem timeline = new TimelineItem();
            TwitterCore core = new TwitterCore();
            XmlDocument document = new XmlDocument();
            String webResult = core.Show(this.connection.UserName, this.connection.Password, screenName,
                                         TwitterCore.OutputFormatType.XML);

            if (webResult == null)
            {
                return null;
            }

            if (webResult.Trim().Length == 0)
            {
                return null;
            }

            document.LoadXml(webResult);

            XmlNodeList xmlTimeline = document.GetElementsByTagName("user");

            User user = new User();
            Status status = new Status();

            status.Id = xmlTimeline[0]["status"]["id"].InnerText;
            status.CreatedAt = xmlTimeline[0]["status"]["created_at"].InnerText;
            status.Text = xmlTimeline[0]["status"]["text"].InnerText;

            user.Id = xmlTimeline[0]["id"].InnerText;
            user.Name = xmlTimeline[0]["name"].InnerText;
            user.ScreenName = xmlTimeline[0]["screen_name"].InnerText;
            user.Location = xmlTimeline[0]["location"].InnerText;
            user.Description = xmlTimeline[0]["description"].InnerText;
            user.ProfileImageUrl = xmlTimeline[0]["profile_image_url"].InnerText;
            user.Url = xmlTimeline[0]["url"].InnerText;
            user.ProtectedStatus = Boolean.Parse(xmlTimeline[0]["protected"].InnerText);
            user.FollowersCount = Int32.Parse(xmlTimeline[0]["followers_count"].InnerText);
            user.ProfileBackgroundColor = xmlTimeline[0]["profile_background_color"].InnerText;
            user.ProfileTextColor = xmlTimeline[0]["profile_text_color"].InnerText;
            user.ProfileLinkColor = xmlTimeline[0]["profile_link_color"].InnerText;
            user.ProfileSidebarFillColor = xmlTimeline[0]["profile_sidebar_fill_color"].InnerText;
            user.ProfileSidebarBorderColor = xmlTimeline[0]["profile_sidebar_border_color"].InnerText;
            user.FriendsCount = Int32.Parse(xmlTimeline[0]["friends_count"].InnerText);
            user.CreatedAt = xmlTimeline[0]["created_at"].InnerText;
            user.FavouritesCount = Int32.Parse(xmlTimeline[0]["favourites_count"].InnerText);
            user.UtcOffset = xmlTimeline[0]["utc_offset"].InnerText;
            user.TimeZone = xmlTimeline[0]["time_zone"].InnerText;
            user.ProfileBackgroundImageUrl = xmlTimeline[0]["profile_background_image_url"].InnerText;
            user.ProfileBackgroundTile = Boolean.Parse(xmlTimeline[0]["profile_background_tile"].InnerText);
            user.StatusesCount = Int32.Parse(xmlTimeline[0]["statuses_count"].InnerText);
            user.Notifications = xmlTimeline[0]["notifications"].InnerText;
            user.Verified = Boolean.Parse(xmlTimeline[0]["verified"].InnerText);

            return new TimelineItem(user, status);
        }
    }
}